package huawei;

import java.util.Arrays;

/**
 * @author: yfq
 * @date: 2023/3/13 9:08
 * @description:
 */
public class InsertSort {
    public static void main(String[] args) {
        int[] arr = {2, 5, 4, 3, 2, 1};
        sort(arr);

        System.out.println(Arrays.toString(arr));
    }

    public static void sort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int temp = arr[i + 1];

            int index = i + 1;
            for (int j = i; j >= 0; j--) {
                if (arr[j] > temp) {
                    arr[j + 1] = arr[j];
                    index--;
                } else {
                    break;
                }
            }
            arr[index] = temp;
        }
    }
}
